/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package model;


import java.util.logging.Level;
import java.util.logging.Logger;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;


/**
 *
 * @author Stupi
 */
public class Calculation {
    
    private int xValue;
    private double yValue;
    private String formula;
    

    
    
    

    public void setFormula(String formula) {
        this.formula = formula;
    }

    public String getFormula() {
        return formula;
    }

    public double getyValue() {
        return yValue;
    }

    public void setyValue(int yValue) {
        this.yValue = yValue;
    }
    
    
    public void calculateYValue (double  x) {
        try {
            
            
            ScriptEngineManager mgr = new ScriptEngineManager();
            ScriptEngine engine = mgr.getEngineByName("JavaScript"); 
            
            String yString = engine.eval(formula.replace("x", x+"")).toString();
            
            
            yValue = Double.parseDouble(yString);
            
            
            
            
        }catch (Exception e) {
            
        }
        
        
    }
    
    
}
